﻿using Permission.Abstract;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
namespace Permission;

public class PermissionDefinitionManager : IPermissionDefinitionManager//, ISingletonDependency
{
    protected IDictionary<string, PermissionGroupDefinition> PermissionGroupDefinitions => _lazyPermissionGroupDefinitions.Value;
    private readonly Lazy<Dictionary<string, PermissionGroupDefinition>> _lazyPermissionGroupDefinitions;

    protected IDictionary<string, PermissionDefinition> PermissionDefinitions => _lazyPermissionDefinitions.Value;
    private readonly Lazy<Dictionary<string, PermissionDefinition>> _lazyPermissionDefinitions;

    //  protected AbpPermissionOptions Options { get; }

    private readonly IServiceProvider _serviceProvider;

    public PermissionDefinitionManager(
        //  IOptions<AbpPermissionOptions> options,
        IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
        //  Options = options.Value;

        _lazyPermissionDefinitions = new Lazy<Dictionary<string, PermissionDefinition>>(
            CreatePermissionDefinitions,
            isThreadSafe: true
        );

        _lazyPermissionGroupDefinitions = new Lazy<Dictionary<string, PermissionGroupDefinition>>(
            //   CreatePermissionGroupDefinitions,
            isThreadSafe: true
        );
    }

    public virtual PermissionDefinition Get(string name)
    {
        var permission = GetOrNull(name);

        if (permission == null)
        {
            throw new Exception("未定义权限: " + name);
        }

        return permission;
    }

    public virtual PermissionDefinition GetOrNull(string name)
    {


        return PermissionDefinitions.Values.First(c => c.Name == name);
    }

    public virtual IReadOnlyList<PermissionDefinition> GetPermissions()
    {
        return PermissionDefinitions.Values.ToImmutableList();
    }

    public IReadOnlyList<PermissionGroupDefinition> GetGroups()
    {
        return PermissionGroupDefinitions.Values.ToImmutableList();
    }

    protected virtual Dictionary<string, PermissionDefinition> CreatePermissionDefinitions()
    {
        var permissions = new Dictionary<string, PermissionDefinition>();

        foreach (var groupDefinition in PermissionGroupDefinitions.Values)
        {
            foreach (var permission in groupDefinition.Permissions)
            {
                AddPermissionToDictionaryRecursively(permissions, permission);
            }
        }

        return permissions;
    }

    protected virtual void AddPermissionToDictionaryRecursively(
        Dictionary<string, PermissionDefinition> permissions,
        PermissionDefinition permission)
    {
        if (permissions.ContainsKey(permission.Name))
        {
            throw new Exception("重复的权限名: " + permission.Name);
        }

        permissions[permission.Name] = permission;

        foreach (var child in permission.Children)
        {
            AddPermissionToDictionaryRecursively(permissions, child);
        }
    }

    //protected virtual Dictionary<string, PermissionGroupDefinition> CreatePermissionGroupDefinitions()
    //{
    //    using (var scope = _serviceProvider.CreateScope())
    //    {
    //        var context = new PermissionDefinitionContext(scope.ServiceProvider);

    //        var providers = Options
    //                .DefinitionProviders
    //                .Select(p => scope.ServiceProvider.GetRequiredService(p) as IPermissionDefinitionProvider)
    //                .ToList();

    //        foreach (var provider in providers)
    //        {
    //            provider.PreDefine(context);
    //        }

    //        foreach (var provider in providers)
    //        {
    //            provider.Define(context);
    //        }

    //        foreach (var provider in providers)
    //        {
    //            provider.PostDefine(context);
    //        }

    //        return context.Groups;
    //    }
    //}
}
